package com.lyb.spzx.manager.service.impl;

import com.lyb.spzx.manager.helper.MenuHelper;
import com.lyb.spzx.manager.mapper.SysMenuMapper;
import com.lyb.spzx.manager.mapper.SysRoleMenuMapper;
import com.lyb.spzx.manager.service.SysRoleMenuService;
import com.lyb.spzx.model.dto.system.AssginMenuDto;
import com.lyb.spzx.model.dto.system.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Map<String, Object> findRoleMenuByRoleId(Long roleId) {

        //查询所有菜单数据
        List<SysMenu> allMenu = sysMenuMapper.findAllMenu();
        List<SysMenu> sysMenus = MenuHelper.buildTrees(allMenu);
        //查询该角色下的所有菜单数据
        List<Long> roleIds = sysRoleMenuMapper.findRoleMenuByRoleId(roleId);

        Map<String, Object> map = new HashMap<>();
        map.put("sysMenuList",sysMenus);
        map.put("roleMenuIds",roleIds);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //根据角色id删除其对应的菜单数据
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if (!CollectionUtils.isEmpty(menuIdList)){
            //动态SQL
            sysRoleMenuMapper.doAssign(assginMenuDto);
        }

    }
}
